package com.ygswglxt.salary.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ygswglxt.salary.entity.SalaryRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SalaryMapper extends BaseMapper<SalaryRecord> {
    
    /**
     * 获取指定用户的薪资记录（联表查询员工名称）
     */
    @Select("SELECT sr.*, COALESCE(u.name, u.username) as userName " +
            "FROM salary_record sr " +
            "LEFT JOIN sys_user u ON sr.user_id = u.id " +
            "WHERE sr.user_id = #{userId} " +
            "ORDER BY sr.month DESC, sr.create_time DESC")
    List<SalaryRecord> selectByUserIdWithUserName(Long userId);
    
    /**
     * 获取所有薪资记录（联表查询员工名称）
     */
    @Select("SELECT sr.*, COALESCE(u.name, u.username) as userName " +
            "FROM salary_record sr " +
            "LEFT JOIN sys_user u ON sr.user_id = u.id " +
            "ORDER BY sr.month DESC, sr.create_time DESC")
    List<SalaryRecord> selectAllWithUserName();
    
    /**
     * 获取团队薪资（联表查询员工名称）
     */
    @Select("SELECT sr.*, COALESCE(u.name, u.username) as userName " +
            "FROM salary_record sr " +
            "LEFT JOIN sys_user u ON sr.user_id = u.id " +
            "ORDER BY sr.month DESC, sr.create_time DESC")
    List<SalaryRecord> selectTeamSalaryWithUserName();
}

